package com.jjh.jhwebserver.util;

import com.jjh.jhwebserver.annation.WebServlet;

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;

/**
 * @author JJH
 */
public class FileUtils {

    /**
     * 返回被@WebServlet注解标注的类的字节码集合
     *
     * @return
     */
    public static Map<String, Class<?>> getServlets() {
          HashMap<String,Class<?>> map = new HashMap<>();

        List<File> list = getClassFileByAnnatation(WebServlet.class);

        for (File file : list) {
            String path = file.getPath();
            String className = getClassName(path);
            Class<?> aClass = null;
            try {
                aClass = Class.forName(className);
                WebServlet annotation = aClass.getAnnotation(WebServlet.class);
                String url = annotation.path();
                map.put(url,aClass);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        return map;
    }


    public static String getClassName(String path) {
        String replace = path.replace("\\", ".");
        int index = replace.lastIndexOf("classes");
        String substring = replace.substring(index + 8);
        String classname = substring.substring(0, substring.lastIndexOf("class") - 1);
        return classname;
    }

    public static void listFile(File file, List<File> fileList) {
        if (!file.isDirectory()) {
            fileList.add(file);
        } else {
            File[] files = file.listFiles();
            File[] var3 = files;
            int var4 = files.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                File subFile = var3[var5];
                listFile(subFile, fileList);
            }
        }

    }

    public static List<File> getClassFileByPackage(String[] packagePaths) {
        List<File> fileList = new ArrayList();
        ArrayList<File> classFileList = new ArrayList();
        String[] var3 = packagePaths;
        int var4 = packagePaths.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String packge = var3[var5];
            String replace = packge.replace(".", "/");
            URL url = ClassLoader.getSystemResource(replace);
            String path = url.getPath();
            File file = new File(path);
            listFile(file, fileList);
            Iterator var11 = fileList.iterator();

            while(var11.hasNext()) {
                File fiLe = (File)var11.next();
                String name = fiLe.getName();
                if (name.endsWith(".class")) {
                    classFileList.add(fiLe);
                }
            }
        }

        return classFileList;
    }

    public static List<File> getClassFiles() {
        URL url = Thread.currentThread().getContextClassLoader().getResource("");
        String path = url.getPath();
        File file = new File(path);
        List<File> fileList = new ArrayList();
        listFile(file, fileList);
        ArrayList<File> classFileList = new ArrayList();
        Iterator var5 = fileList.iterator();

        while(var5.hasNext()) {
            File fiLe = (File)var5.next();
            String name = fiLe.getName();
            if (name.endsWith(".class")) {
                classFileList.add(fiLe);
            }
        }

        return classFileList;
    }



    public static List<File> getClassFileByAnnatation(Class<WebServlet> webServletClass) {
        List<File> list = new ArrayList();
        List<File> classFiles = getClassFiles();
        Iterator var3 = classFiles.iterator();

        while(var3.hasNext()) {
            File classFile = (File)var3.next();
            String path = classFile.getPath();
            String className = getClassName(path);
            Class<?> aClass = null;

            try {
                aClass = Class.forName(className);
            } catch (ClassNotFoundException var9) {
                throw new RuntimeException(var9);
            }

            Annotation annotation = aClass.getAnnotation(webServletClass);
            if (annotation != null) {
                list.add(classFile);
            }
        }

        return list;
    }

    /**
     * 返回webapps目录下所有的静态资源
     * @return
     */
    public static List<File> getWebappsStaticFiles(){
        List<File> files = new ArrayList<>();
        List<File> resFiles = new ArrayList<>();
        File file = new File("src/main/webapp");
        listFile(file,files);

        for (File file1 : files) {
            String name = file1.getName();
            if(!name.endsWith("web.xml")){
               resFiles.add(file1);
            }
        }

        return resFiles;
    }

//    public static void main(String[] args) {
//        FileUtils.getWebappsStaticFiles();
//    }

    /**
     * 返回文件的字节数组
     * @param file 文件
     * @return 字节数组
     */
    public static byte[] getFileByte(File file){

        byte[] bytes = new byte[(int)file.length()];

        InputStream in = null;
        try {
            in = new FileInputStream(file);
            in.read(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                assert in != null;
                in.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
       return bytes;
    }

    /**
     * 获取webapp目录下的某一个文件
     * @param name
     * @return
     */
    public static File getWebAppOneFile(String name){
        List<File> files = getWebappsStaticFiles();
        for (File file : files) {
           if (name.contains(file.getName())){
               return file;
           }
        }
        return null;
    }


}




